package the.workshop.milktea.interfaces.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.util.TextUtils;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.workshop.milktea.infrastructure.common.CupTypeEnum;
import the.workshop.milktea.infrastructure.common.IceTypeEnum;
import the.workshop.milktea.infrastructure.common.ProductTypeEnum;
import the.workshop.milktea.infrastructure.common.SugarTypeEnum;
import the.workshop.milktea.interfaces.req.ProductAddReq;
import the.workshop.milktea.interfaces.req.ProductModifyReq;
import the.workshop.milktea.interfaces.req.ProductPageReq;
import the.workshop.milktea.interfaces.req.ProductRemoveReq;
import the.workshop.milktea.interfaces.resp.ProductGetResp;
import the.workshop.milktea.interfaces.resp.ProductPageResp;
import the.workshop.milktea.service.TeaProductService;
import the.workshop.milktea.service.dto.ProductAddDTO;
import the.workshop.milktea.service.dto.ProductGetDTO;
import the.workshop.milktea.service.dto.ProductModifyDTO;
import the.workshop.milktea.service.dto.ProductPageDTO;

import javax.annotation.Resource;

/**
 * <p>
 * 奶茶产品 前端控制器
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Api(tags = "Tea")
@RestController
@RequestMapping("/milktea/product")
public class TeaProductController {

    @Resource
    private TeaProductService teaProductService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody ProductAddReq req) {
        ProductAddDTO dto = new ProductAddDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(teaProductService.add(dto));
    }

    @PostMapping("/modify")
    public APIResult<Boolean> modify(@RequestBody ProductModifyReq req) {
        ProductModifyDTO dto = new ProductModifyDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(teaProductService.modify(dto));
    }

    @PostMapping("/remove")
    public APIResult<Boolean> remove(@RequestBody ProductRemoveReq req) {
        teaProductService.removeByIds(req.getIds(), UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @GetMapping("/page")
    public APIResult<QueryResult<ProductPageResp>> page(ProductPageReq req) {
        ProductPageDTO dto  = new ProductPageDTO();
        BeanUtils.copyProperties(req, dto);
        dto.setPageNum(req.getPageNum());
        dto.setPageSize(req.getPageSize());
        QueryResult<ProductPageResp> queryResult = teaProductService.pageList(dto, req.getPageNum(), req.getPageSize());
        if (!CollectionUtils.isEmpty(queryResult.getItems())) {
            queryResult.getItems().stream().forEach(it -> {
                it.setCupTypeName(CupTypeEnum.getName(it.getCupType()));
                it.setIceTypeName(IceTypeEnum.getName(it.getIceType()));
                it.setSugarTypeText(SugarTypeEnum.getName(it.getSugarType()) + " " + TextUtils.nullToEmpty(it.getSugarWeight()) + "g");
                it.setProductTypeName(ProductTypeEnum.getName(it.getProductType()));
            });
        }
        return APIResult.success(queryResult);
    }

    @GetMapping("/{id}")
    public APIResult<ProductGetResp> get(@PathVariable(value = "id") Integer id) {
        ProductGetResp resp = null;
        ProductGetDTO dto = teaProductService.get(id);
        if (dto != null) {
            resp = new ProductGetResp();
            BeanUtils.copyProperties(dto, resp);
        }
        return APIResult.success(resp);
    }


}
